using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using StupidSpaceThing;

namespace RacerGame
{
	internal class Hive : Structure
	{		
		private FactionEntity production;
		private double productionStart;
		private Queue<FactionEntity> productionQueue = new Queue<FactionEntity>();
		public int Queued { 
			get { 
				int queued = productionQueue.Count;
				if( production != null ) {
					queued++;
				}
				return queued;
			}
		}

		public Hive(Vector3 position)
			: base(new HiveEntity(position)) {			
		}

		public void Produce(FactionEntity entity) {
			if( Faction.Credits < entity.ProductionCost ) return;
			Faction.Credits -= entity.ProductionCost;
			if( entity is Unit ) {
				entity.Entered = false;
				if( entity is DataTrain ) {
					entity.Position = Position + new Vector3(0, -10, 0);
				} else {
					entity.Position = Position + new Vector3(-5, 5, -5);
				}
				entity.Entity.UpdatePositionMatrix();
				Faction.Add(entity);					
			} else {
			}
			if( entity.Collision ) {
				entity.BodyEnabled = false;
			}
			entity.Entity.ScaleFactor = 0;
			productionQueue.Enqueue(entity);
		}

		public override void Update(RealGameTime gameTime)
		{			
			if( production != null ) {
				float remaining = (float)((productionStart + production.ProductionTime) - gameTime.TotalRealGameTime.TotalMilliseconds);
				if( remaining <= 0 || production.Entity.Dead ) {
					if( !production.Entity.Dead ) {
						production.Entity.ScaleFactor = 1;
						if( production is Unit && !(production is DataTrain) ) {
							List<Vector3> destinations = new List<Vector3>();
							destinations.Add(production.Position + new Vector3(0, 0, -7));
							destinations.Add(new Vector3(production.Position.X, Map.MaxHeight, production.Position.Z - 7));
							if( Faction == Faction.Green ) {
								destinations.Add(new Vector3(production.Position.X - 7, Map.MaxHeight, production.Position.Z - 7));
								destinations.Add(new Vector3(production.Position.X - 7, Map.MaxHeight, production.Position.Z + 15));
							} else if( Faction == Faction.Yellow ) {
								destinations.Add(new Vector3(production.Position.X + 17, Map.MaxHeight, production.Position.Z - 7));
								destinations.Add(new Vector3(production.Position.X + 17, Map.MaxHeight, production.Position.Z + 15));
							}
							PathMover.Add(production, destinations.ToArray());
						}
					}
					production = null;
				} else {
					production.Entity.ScaleFactor = MathHelper.Lerp(1, 0, remaining / production.ProductionTime);
				}
			}
			if( productionQueue.Count > 0 && production == null ) {				
				production = productionQueue.Dequeue();
				if( production is Structure ) {
					if( RacerGame.CurMap.GetCell(production).Occupier != null ) {
						Faction.Credits += production.ProductionCost;
						production = null;
					} else {
						RacerGame.PlayCue("building", RacerGame.Listener, production.AudioEmitter);
						Faction.AddNext(production);
						RacerGame.CurMap.Occupy(production, false);
						production.BodyEnabled = true;
					}
				}
				productionStart = gameTime.TotalRealGameTime.TotalMilliseconds;				
			}
 			base.Update(gameTime);
		}
	}
}
